﻿// Copyright 2007, 2008 - Ariel Yaroshevich (a.k.a blackrussian) (hamlet@inter.net.il)
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using SharpMap.Wpf.Rendering;
using System.Windows;
using System.Windows.Media;

namespace SharpMap.Presentation.Wpf.Converters
{
    /// <summary>
    /// Convertes from SharpMap.Styles.StyleColor to System.Windows.Media.Color
    /// or System.Windows.Media.SolidColorBrush
    /// </summary>
    public class ColorConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is SharpMap.Styles.StyleColor)
            {
                SharpMap.Styles.StyleColor sourceColor = (SharpMap.Styles.StyleColor)value ;
                if ( targetType.IsAssignableFrom(typeof(System.Windows.Media.Color)))
                {
                    return ViewConverter.Convert(sourceColor);
                }
                if (targetType.IsAssignableFrom(typeof(System.Windows.Media.Brush)))
                {
                    return new SolidColorBrush(  ViewConverter.Convert(sourceColor));
                }

                if (targetType == typeof(string))
                {
                    return ViewConverter.Convert(sourceColor).ToString();
                }
            }

            return DependencyProperty.UnsetValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is System.Windows.Media.Color)
            {
                System.Windows.Media.Color sourceColor = (System.Windows.Media.Color)value ;
                if (targetType.IsAssignableFrom(typeof(SharpMap.Styles.StyleColor)))
                {
                    return ViewConverter.Convert(sourceColor);
                }
            }
            if (value is string)
            {
                try
                {
                    System.Windows.Media.Color clr = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(value as string);

                    return ViewConverter.Convert(clr);
                }
                catch (InvalidCastException)
                {
                    return DependencyProperty.UnsetValue;
                }
            }
            return DependencyProperty.UnsetValue;
        }

        #endregion
        static System.Windows.Media.ColorConverter _wpfColorConverter = new System.Windows.Media.ColorConverter();

        static ColorConverter _instance = null;
        public static ColorConverter Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ColorConverter();
                }
                return _instance;
            }

        }
    }
}
